package cn.xhjava.crm.workbench.controller;

import cn.xhjava.crm.commons.ReturnObject;
import cn.xhjava.crm.commons.SysContants;
import cn.xhjava.crm.commons.util.ExceptionUtil;
import cn.xhjava.crm.commons.util.UUIDUtils;
import cn.xhjava.crm.workbench.domain.Activity;
import cn.xhjava.crm.workbench.domain.ClueActivityRelation;
import cn.xhjava.crm.workbench.service.ActivityService;
import cn.xhjava.crm.workbench.service.ClueActivityRelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:   XiaHu
 * Date:     2023/6/27
 * 线索 - 市场活动关系
 */
@Controller
public class ClueActivityRelationController {
    private static final Logger log = LoggerFactory.getLogger(ClueActivityRelationController.class);
    @Autowired
    private ActivityService activityService;

    @Autowired
    private ClueActivityRelationService clueActivityRelationService;

    // 绑定ClueActivity
    @RequestMapping("/workbench/clue/relation/bundActivity")
    public @ResponseBody
    Object addBund(String[] activityId, String clueId) {
        //封装参数
        ClueActivityRelation car = null;
        List<ClueActivityRelation> relationList = new ArrayList<>();
        for (String ai : activityId) {
            car = new ClueActivityRelation();
            car.setActivityId(ai);
            car.setClueId(clueId);
            car.setId(UUIDUtils.getUUID());
            relationList.add(car);
        }

        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service方法，批量保存线索和市场活动的关联关系
            int ret = clueActivityRelationService.addList(relationList);

            if (ret > 0) {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_SUCCESS);

                List<Activity> activityList = activityService.selectDetailListById(activityId);
                returnObject.setRetData(activityList);
            } else {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试....");
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
            returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试....");
        }

        return returnObject;
    }

    // 解除绑定Clue Activity
    @RequestMapping("/workbench/clue/relation/unbundActivity")
    public @ResponseBody
    Object deleteBund(ClueActivityRelation relation) {
        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service层方法，删除线索和市场活动的关联关系
            int ret = clueActivityRelationService.deleteClueActivityRelationByClueIdActivityId(relation);

            if (ret > 0) {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统忙，请稍后重试....");
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.getStackTrace(e));
            returnObject.setCode(SysContants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统忙，请稍后重试....");
        }

        return returnObject;
    }
}
